<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-title">
        <h2>智能社团管理系统</h2>
      </div>
      
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" label-position="top">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" prefix-icon="el-icon-lock" show-password></el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" class="full-width" :loading="loading" @click="handleLogin">登录</el-button>
        </el-form-item>
        
        <div class="login-options">
          <span>还没有账号？</span>
          <router-link to="/register">立即注册</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/store/user'

export default {
  name: 'Login',
  setup() {
    const router = useRouter()
    const route = useRoute()
    const userStore = useUserStore()
    const redirect = ref('')

    // 获取重定向参数
    onMounted(() => {
      if (route.query.redirect) {
        redirect.value = route.query.redirect
      }
    })
    
    // 登录表单
    const loginForm = reactive({
      username: '',
      password: ''
    })
    
    // 表单验证规则
    const loginRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ]
    }
    
    const loginFormRef = ref(null)
    const loading = ref(false)
    
    // 登录提交
    const handleLogin = async () => {
      if (!loginFormRef.value) return
      
      await loginFormRef.value.validate(async (valid) => {
        if (valid) {
          loading.value = true
          
          try {
            const res = await userStore.loginAction(loginForm)
            
            if (res.code === 200) {
              ElMessage.success('登录成功')
              
              // 如果有重定向地址，则跳转到重定向地址
              if (redirect.value) {
                router.push(redirect.value)
              } else {
                router.push('/home')
              }
            } else {
              ElMessage.error(res.message || '登录失败')
            }
          } catch (error) {
            console.error('登录错误:', error)
            ElMessage.error(error.message || '登录失败')
          } finally {
            loading.value = false
          }
        } else {
          return false
        }
      })
    }
    
    // 跳转到注册页
    const goToRegister = () => {
      router.push('/register')
    }
    
    return {
      loginForm,
      loginRules,
      loginFormRef,
      loading,
      handleLogin,
      goToRegister
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('../assets/back.png');
  /* background-image: url("https://preview.qiantucdn.com/58pic/x7/6T/At/HI/cav46zq83miys1wtp7b2rjlofk9g5nxh_PIC2018_png_aiys_PIC2018.jpg%21qt_h320"); */
  background-size: cover;
  background-repeat: no-repeat;
}

.login-box {
  width: 400px;
  padding: 30px;
  background-color: rgba(255, 255, 255, 0.1); /* 10% 不透明度 */
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(5px); /* 可选：添加毛玻璃效果 */
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
}

.login-title h2 {
  font-size: 24px;
  /* 修改文字颜色为白色，在透明背景下更清晰 */
  color: white;
  margin: 0;
  /* 可选：添加文字阴影增强可读性 */
  text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.5);
}

.login-options {
  margin-top: 15px;
  text-align: center;
  font-size: 14px;
  /* 修改文字颜色为白色 */
  color: white;
}

.login-options a {
  color: #a0cfff; /* 使用更亮的蓝色在透明背景下更清晰 */
  text-decoration: none;
}

/* 如果需要，可以添加样式来调整表单元素的颜色 */
:deep(.el-form-item__label) {
  color: white !important;
}

:deep(.el-input__inner) {
  background-color: rgba(255, 255, 255, 0.9) !important;
}
</style>